<template>
  <section>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin:20px;width:60%;min-width:600px;">
      <el-form-item label="原密码" prop="passWord">
        <el-input type="password" v-model="form.passWord" auto-complete="off" placeholder="原密码"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassWord">
        <el-input type="password" v-model="form.newPassWord" @change="cgNPw" auto-complete="off"
                  placeholder="新密码"></el-input>
        <!--<div>
          <span v-if="lvTxt!=''" :style="`color: ${lvClass};`">强度 : {{lvTxt}}</span>
        </div>-->
      </el-form-item>
      <el-form-item label="重复新密码" prop="newPassWord2">
        <el-input type="password" v-model="form.newPassWord2" auto-complete="off" placeholder="重复新密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePW">立即修改</el-button>
        <!--<el-button @click="cancelUpDate">取消</el-button>-->
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
  import util from '../../common/util'
  import {mapGetters} from 'vuex'
  import NProgress from 'nprogress'
  import dao from '../../common/dao'

  export default {
    components: {},
    computed: {
      ...mapGetters([
        'getUser'
      ])
    },
    data() {
      return {
        form: {
          passWord: '',
          newPassWord: '',
          newPassWord2: '',
        },
        lvClass: 0,
        lvTxt: '',
        timeout: null,
        rules: {
          passWord: [
            {required: true, message: '请输入原密码', trigger: 'blur'},
          ], newPassWord: [
            {required: true, message: '请输入新密码', trigger: 'blur'},
            {
              validator: (rule, value, callback, source, options) => {
                if (value == this.form.passWord) callback(new Error('不能与原密码一致!'));
                else callback()
              }, trigger: 'blur'
            },
            {validator: util.validatePWS, trigger: 'blur'}
          ], newPassWord2: [
            {required: true, message: '请重复新密码!', trigger: 'blur'},
            {
              validator: (rule, value, callback, source, options) => {
                if (value != this.form.newPassWord) callback(new Error('两次输入密码不一致!'));
                else callback()
              }, trigger: 'blur'
            },
          ],
        },
      }
    },
    methods: {
      updatePW(){
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$confirm('此操作将修改密码, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              return dao.updatePasswd(
                {
                  user_name: this.getUser.user_name,
                  password_old: util.rsaEncryption(this.form.passWord),
                  password: util.rsaEncryption(this.form.newPassWord),
                  passworVerify: util.rsaEncryption(this.form.newPassWord2),
                }
              )
            }).then(res => {
              if (res.body && res.body.retCode == 200) {
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                });
                this.$router.replace('/login');

              } else {
                this.$message({
                  type: 'info',
                  message: '修改失败!'
                });
              }
            }).catch((res) => {
              this.$message({
                type: 'info',
                message: '已取消修改'
              });
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      cancelUpDate(){
      },
      cgNPw(){
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          this.testStrength()
        }, 1000);
      },
      testStrength(){
        let val = this.form.newPassWord;
        let obj = util.pwStrength(val);
        this.lvClass = obj.lvClass;
        this.lvTxt = obj.lvTxt
      },
    },
    mounted: function () {

    }
  }
</script>

<style scoped>

</style>